package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Driver;
import java.time.Duration;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class demo01 {
    public WebDriver webDriver;

    @BeforeClass
    public void before() {
        DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
        cap.setCapability("ignoreProtectedModeSettings", true);
        cap.setCapability("ignoreZoomSetting", true);//忽略原有系统的缩放级别
        InternetExplorerOptions options = new InternetExplorerOptions();
        options.merge(cap);
        System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer_Win32_3.4.0\\IEDriverServer.exe");
        //创建浏览器实例
        webDriver = new InternetExplorerDriver(options);
    }

    @Test
    public void openbaidu() throws InterruptedException {
        webDriver.manage().window().maximize();
        webDriver.get("https://www.baidu.com");

        webDriver.findElement(By.id("kw")).sendKeys("hello,world");
        webDriver.findElement(By.id("su")).click();
        Thread.sleep(3000);
    }

    @Test
    public void openxiechengByXPath() throws InterruptedException {
        webDriver.get("https://www.ctrip.com");
        webDriver.findElement(By.xpath("//a[@class=\"cui_nav_has\" and text()=\"酒店\"]")).click();
        Thread.sleep(3000);
    }

    @Test
    public void openbaiduByClass() throws InterruptedException {
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.name("wd")).sendKeys("hello,world");
        webDriver.findElement(By.className("s_btn")).click();
        Thread.sleep(3000);
    }

    /**
     * 模拟鼠标滑动
     *
     * @throws InterruptedException
     */
    @Test
    public void openbaiduByActions() throws InterruptedException {
        webDriver.get("https://www.baidu.com");
        Actions actions = new Actions(webDriver);
        WebElement moreBtn = webDriver.findElement(By.linkText("更多"));
        actions.moveToElement(moreBtn);
        Thread.sleep(3000);
        WebElement mp3link = webDriver.findElement(By.xpath("//a[@name=\"tj_mp3\"]"));
        actions.moveToElement(mp3link).click().perform();
        Thread.sleep(3000);
    }

    /**
     * 模拟鼠标拖动
     *
     * @throws InterruptedException
     */
    @Test
    public void openbaiduByDrop() throws InterruptedException {
        webDriver.get("https://map.baidu.com/");
        WebElement mask = webDriver.findElement(By.id("mask"));
        Actions actions = new Actions(webDriver);
        actions.moveToElement(mask).contextClick().pause(3000);
        WebElement cmitem_start = webDriver.findElement(By.id("cmitem_start"));
        actions.moveToElement(cmitem_start).click();
        actions.clickAndHold();
        actions.moveByOffset(200, 100);
        actions.release();
        actions.contextClick();
        actions.pause(3000);
        WebElement end = webDriver.findElement(By.id("cmitem_end"));
        actions.moveToElement(end).click();
        actions.pause(3000);
        actions.perform();
    }

    /**
     * 设置隐式等待时间
     *
     * @throws InterruptedException
     */
    @Test
    public void openbaiduByTimeOut() {
        webDriver.get("https://www.juhe.cn/");
        //设置最长等待时间为10s
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        WebElement frame = webDriver.findElement(By.id("layui-layer-iframe1"));
        webDriver.switchTo().frame(frame);
        webDriver.findElement(By.id("username")).sendKeys("12345");
    }

    /**
     * 切换窗口
     */
    @Test
    public void openbaiduBySwitch() throws InterruptedException {
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.linkText("学术")).click();
        //获取当前浏览器窗口
        String currentWindow = webDriver.getWindowHandle();

        Set<String> allWindows = webDriver.getWindowHandles();
        for (String window : allWindows) {
            if (window != currentWindow) {
                webDriver.switchTo().window(window);
            }
        }
        //学术上进行自动化
        webDriver.findElement(By.id("kw")).sendKeys("hello,world");
        webDriver.switchTo().window(currentWindow);
        webDriver.findElement(By.id("kw")).sendKeys("hhhhh");
        Thread.sleep(3000);
    }

    /**
     * 切换iframe
     *
     * @throws InterruptedException
     */
    @Test
    public void openbaiduByIframe() throws InterruptedException {
        webDriver.get("https://login.anjuke.com/login/form");
        WebElement iframe = webDriver.findElement(By.id("iframeLoginIfm"));
        webDriver.switchTo().frame(iframe);
        webDriver.findElement(By.id("phoneIpt")).sendKeys("1234564");
    }

    /**
     * 设置显示时间
     *
     * @throws InterruptedException
     */
    @Test
    public void openbaiduByOutTime() throws InterruptedException {
        webDriver.get("https://www.juhe.cn/");
        WebDriverWait wait = new WebDriverWait(webDriver, 10, 10);
        WebElement frame = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("layui-layer-iframe1")));
        webDriver.switchTo().frame(frame);
        webDriver.findElement(By.id("username")).sendKeys("123456789");
    }

    @AfterClass
    public void after() {
        webDriver.quit();
    }

}
